<template>
    <div class="wrapper">
        <v-head></v-head>
        <v-sidebar></v-sidebar>
        <div class="content-box" :class="{'content-collapse':collapse}">
            <v-tags></v-tags>
            <div class="content">
                <transition name="move" mode="out-in">
                    <keep-alive :include="tagsList">
                        <router-view></router-view>
                    </keep-alive>
                </transition>
                <el-backtop target=".content"></el-backtop>
            </div>
        </div>
    </div>
</template>

<script>
import vHead from './Header.vue';
import vSidebar from './Sidebar.vue';
import vTags from './Tags.vue';
import bus from './bus';
import {mapMutations} from 'vuex';
import funs from "../../utils/function.js"
export default {
    data() {
        return {
            tagsList: [],
            collapse: false
        };
    },
    components: {
        vHead,
        vSidebar,
        vTags
    },
    methods:{
        ...mapMutations(['setUserInfo','setToken','setMenus']),
        getUserInfo(){
            const _this   = this;
            //初始化用户信息
            let p = (async function(){ return await funs.request.get("/system/index/index")})();
            p.then((response)=>{
                if(response.code===0){
                    _this.setUserInfo(response.data.userInfo);
                    _this.setMenus(response.data.menuList);
                }else{
                    _this.$message.error(response.msg+"请登陆。");
                    localStorage.removeItem('token');
                    sessionStorage.removeItem('token');
                    _this.setToken('');
                    _this.$router.push('/login');
                }
            }).catch(err=>{
                console.log(err);
                localStorage.removeItem('token');
                sessionStorage.removeItem('token');
                _this.setToken('');
                _this.$router.push('/login');
            });
        }
    },
    created() {

        bus.$on('collapse-content', msg => {
            this.collapse = msg;
        });

        // 只有在标签页列表里的页面才使用keep-alive，即关闭标签之后就不保存到内存中了。
        bus.$on('tags', msg => {
            let arr = [];
            for (let i = 0, len = msg.length; i < len; i++) {
                msg[i].name && arr.push(msg[i].name);
            }
            this.tagsList = arr;
        });
        this.getUserInfo();
    },
};
</script>
<style>
    .table_btn{
        margin-bottom: 10px;
        overflow:hidden;
        height: 32px;
    }

    .span_btn{margin-right: 15px;}
    .mag_top{
        margin-top: 10px;
    }
    .table-label{
        color:#606266;
        font-size:12px;
    }

    .menu-input {
        width:300px;
        margin:0 10px;
    }

    .content{
        overflow:scroll;
    }
</style>
